package com.jldx.oa.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;

import com.google.zxing.WriterException;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jldx.common.NoUtil;
import com.jldx.common.QRCodeUtil;
import com.jldx.common.Result;
import com.jldx.oa.bean.BuyOrder;
import com.jldx.oa.bean.Goods;
import com.jldx.oa.bean.OrderLine;
import com.jldx.oa.bean.SaleOrder;
import com.jldx.oa.bean.User;

public class SaleOrderController extends BaseController{

	//跳转到主页面
	public void index(){
		render("/WEB-INF/views/sale_order.html");
	}
	
	//查询合同单  返回json格式
	public void query(){
		Integer pageNumber = getPage();
		String name = getPara("no");
		Integer limit = getLimit();
		Kv cond = Kv.by("no", name);
		SqlPara sqlPara = Db.getSqlPara("oa.saleOrderList",cond);
		Page<SaleOrder> page = SaleOrder.dao.paginate(pageNumber, limit, sqlPara);
		for(SaleOrder so : page.getList()){
			so.put("state_cn", so.getStateCn());
		}
		Result result = Result.cast(page);
		renderJson(result);
	}
	
	//进入编辑界面
	public void edit(){
		Integer id = getParaToInt("id");
		if(id!=null){
			SaleOrder sale_order = SaleOrder.dao.findById(id);
			setAttr("sale_order", sale_order);
		}
		List<Goods> gs = Goods.dao.find("select * from goods ");
		setAttr("gs", gs);
		List<User> ps = User.dao.find("select * from user where type='customer'");
		setAttr("ps",ps);
		view("sale_order_edit.html");
	}
	
	//海港发货
	public void chuhuo(){
		Integer id = getParaToInt("id");
		String chuhuo = getPara("chuhuo");
		SaleOrder sale_order = SaleOrder.dao.findById(id);
		sale_order.setChuhuo(chuhuo);
		sale_order.setChuhuoTime(new Date());
		sale_order.setState("fahuo");
		sale_order.update();
		//发货时减少库存
		List<OrderLine> lines = sale_order.getLines();
		for(OrderLine l : lines){
			Goods goods = Goods.dao.findById(l.getGoodsId());
			goods.setStoreCount(goods.getStoreCount() - l.getGoodsCount());
			goods.update();
		}
		renderText("suc");
	}
	
	//确认退货
	@Before(Tx.class)
	public void confirmTuihuo(){
		Integer id = getParaToInt("id");
		String checkState = getPara("state");
		
		SaleOrder sale_order = SaleOrder.dao.findById(id);
		sale_order.setState(checkState);
		sale_order.update();
		//退货时增加库存
		List<OrderLine> lines = sale_order.getLines();
		for(OrderLine l : lines){
			Goods goods = Goods.dao.findById(l.getGoodsId());
			goods.setStoreCount(goods.getStoreCount() + l.getGoodsCount());
			goods.update();
		}
		
		renderText("suc");
	}
	
	//确认赔款
	public void confirmPeikuan(){
		Integer id = getParaToInt("id");
		String checkState = getPara("state");
		
		SaleOrder sale_order = SaleOrder.dao.findById(id);
		sale_order.setState(checkState);
		sale_order.update();
		
		renderText("suc");
	}
	
	//保存订单
	@Before(Tx.class)
	public void save(){
		Integer partnerId = getParaToInt("partner_id");
		String strOtherMoney = getPara("other_money");
		
		Float otherMoney = 0.0f;
		try{
			otherMoney = Float.parseFloat(strOtherMoney);
		}catch(Exception e){}
		
		Integer[] goodsIds = getParaValuesToInt("goods_id");
		Integer[] goodsCounts = getParaValuesToInt("goods_count");
		String[] goodsPrices = getParaValues("goods_price");
		
		SaleOrder saleOrder = new SaleOrder();
		saleOrder.setPartnerId(partnerId);
		saleOrder.setUserId(getCurrentUser().getId());
		saleOrder.setCreateTime(new Date());
		saleOrder.setNo(NoUtil.createNo("sale"));
		saleOrder.setOtherMoney(otherMoney);
		saleOrder.setState("draft");
		
		saleOrder.save();
		Integer i = 0;
		float money = 0;
		for(Integer gid : goodsIds){
			OrderLine ol = new OrderLine();
			ol.setOrderId(saleOrder.getId());
			ol.setGoodsId(gid);
			ol.setGoodsCount(goodsCounts[i]);
			ol.setGoodsPrice(Float.parseFloat(goodsPrices[i]));
			ol.setMoney(ol.getGoodsCount()* ol.getGoodsPrice());
			money += ol.getMoney();
			ol.save();
			i++;
		}
		saleOrder.setMoney(money+saleOrder.getOtherMoney());
		saleOrder.update();
		
		renderText("suc");
	}
	
	//删除订单
	@Before(Tx.class)
	public void delete(){
		Integer id = getParaToInt("id");
		boolean result = false;
		SaleOrder saleOrder = SaleOrder.dao.findById(id);
		if(id!=null){
			List<OrderLine> lines = saleOrder.getLines();
			for(OrderLine ol : lines){
				ol.delete();
			}
			result = SaleOrder.dao.deleteById(id);
			
		}
		if(result){
			renderText("suc");
		}else{
			render("fail");
		}
	}
	
}
